{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "98a44163-8ca2-412b-8a46-36156502912b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Setup OpenAI Agent\n",
    "import openai\n",
    "\n",
    "openai.api_key = \"sk-your-key\"\n",
    "from llama_index.agent import OpenAIAgent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f61e4dde-928c-48f4-b9fa-bdb523574aa4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import and initialize our tool spec\n",
    "from llama_hub.tools.gmail.base import GmailToolSpec\n",
    "\n",
    "tool_spec = GmailToolSpec()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8af5e53a-0d18-48cb-9f12-c2245da81edc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create the Agent with our tools\n",
    "agent = OpenAIAgent.from_tools(tool_spec.to_tool_list(), verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2f038a10-0b35-40eb-a24d-88967e814be9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== Calling Function ===\n",
      "Calling function: create_draft with args: {\n",
      "  \"to\": [\"helpdesk@example.com\", \"support@example.com\"],\n",
      "  \"subject\": \"Service Outage\",\n",
      "  \"message\": \"Dear Helpdesk and Support,\\n\\nI wanted to inform you that we are currently experiencing a service outage. Our team is working diligently to resolve the issue as quickly as possible. We apologize for any inconvenience this may cause.\\n\\nThank you for your understanding.\\n\\nBest regards,\\n[Your Name]\"\n",
      "}\n",
      "Got output: {'id': 'r2727919118905591812', 'message': {'id': '18922ad7d6476b9a', 'threadId': '18922ad7d6476b9a', 'labelIds': ['DRAFT']}}\n",
      "========================\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Response(response='I have created a new email draft. Here are the details:\\n\\nDraft ID: r2727919118905591812\\nSubject: Service Outage\\nRecipient(s): helpdesk@example.com, support@example.com\\n\\nMessage:\\nDear Helpdesk and Support,\\n\\nI wanted to inform you that we are currently experiencing a service outage. Our team is working diligently to resolve the issue as quickly as possible. We apologize for any inconvenience this may cause.\\n\\nThank you for your understanding.\\n\\nBest regards,\\n[Your Name]', source_nodes=[], metadata=None)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent.chat(\n",
    "    \"Can you create a new email to helpdesk and support @example.com about a service\"\n",
    "    \" outage\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "82c25451-8327-4adc-a3da-a5f968133c94",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== Calling Function ===\n",
      "Calling function: update_draft with args: {\n",
      "  \"message\": \"Dear Helpdesk and Support,\\n\\nI wanted to inform you that we are currently experiencing a service outage. Our team is working diligently to resolve the issue as quickly as possible. We apologize for any inconvenience this may cause.\\n\\nThank you for your understanding.\\n\\nBest regards,\\nAdam\",\n",
      "  \"draft_id\": \"r2727919118905591812\"\n",
      "}\n",
      "Got output: {'id': 'r2727919118905591812', 'message': {'id': '18922ad901074c7b', 'threadId': '18922ad901074c7b', 'labelIds': ['DRAFT']}}\n",
      "========================\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Response(response='I have updated the draft email. Here are the details:\\n\\nDraft ID: r2727919118905591812\\nSubject: Service Outage\\nRecipient(s): helpdesk@example.com, support@example.com\\n\\nUpdated Message:\\nDear Helpdesk and Support,\\n\\nI wanted to inform you that we are currently experiencing a service outage. Our team is working diligently to resolve the issue as quickly as possible. We apologize for any inconvenience this may cause.\\n\\nThank you for your understanding.\\n\\nBest regards,\\nAdam', source_nodes=[], metadata=None)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent.chat(\"Update the draft so that it's the same but from 'Adam'\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e7137398-5540-44d8-b36b-7c89380c5205",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== Calling Function ===\n",
      "Calling function: get_draft with args: {\n",
      "  \"draft_id\": \"r2727919118905591812\"\n",
      "}\n",
      "Got output: {'id': 'r2727919118905591812', 'message': {'id': '18922ad901074c7b', 'threadId': '18922ad901074c7b', 'labelIds': ['DRAFT'], 'snippet': 'Dear Helpdesk and Support, I wanted to inform you that we are currently experiencing a service outage. Our team is working diligently to resolve the issue as quickly as possible. We apologize for any', 'payload': {'partId': '', 'mimeType': 'text/plain', 'filename': '', 'headers': [{'name': 'Received', 'value': 'from 117506200987 named unknown by gmailapi.google.com with HTTPREST; Tue, 4 Jul 2023 20:52:27 +0000'}, {'name': 'Content-Type', 'value': 'text/plain; charset=\"utf-8\"'}, {'name': 'Content-Transfer-Encoding', 'value': 'quoted-printable'}, {'name': 'MIME-Version', 'value': '1.0'}, {'name': 'To', 'value': 'helpdesk@example.com, support@example.com'}, {'name': 'Subject', 'value': 'Service Outage'}, {'name': 'Date', 'value': 'Tue, 4 Jul 2023 20:52:27 +0000'}, {'name': 'Message-Id', 'value': '<CAC=6HAW6sSt+mnDGOJ8F0h6zscEp_a8CPj=vXKkVZU-spfxkTg@mail.gmail.com>'}, {'name': 'From', 'value': 'adam.hofmann@hypotenuse.ca'}], 'body': {'size': 286, 'data': 'RGVhciBIZWxwZGVzayBhbmQgU3VwcG9ydCwKCkkgd2FudGVkIHRvIGluZm9ybSB5b3UgdGhhdCB3ZSBhcmUgY3VycmVudGx5IGV4cGVyaWVuY2luZyBhIHNlcnZpY2Ugb3V0YWdlLiBPdXIgdGVhbSBpcyB3b3JraW5nIGRpbGlnZW50bHkgdG8gcmVzb2x2ZSB0aGUgaXNzdWUgYXMgcXVpY2tseSBhcyBwb3NzaWJsZS4gV2UgYXBvbG9naXplIGZvciBhbnkgaW5jb252ZW5pZW5jZSB0aGlzIG1heSBjYXVzZS4KClRoYW5rIHlvdSBmb3IgeW91ciB1bmRlcnN0YW5kaW5nLgoKQmVzdCByZWdhcmRzLApBZGFtCg=='}}, 'sizeEstimate': 745, 'historyId': '392124', 'internalDate': '1688503947000'}}\n",
      "========================\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Response(response='Here is the draft email:\\n\\nDraft ID: r2727919118905591812\\nSubject: Service Outage\\nRecipient(s): helpdesk@example.com, support@example.com\\n\\nMessage:\\nDear Helpdesk and Support,\\n\\nI wanted to inform you that we are currently experiencing a service outage. Our team is working diligently to resolve the issue as quickly as possible. We apologize for any inconvenience this may cause.\\n\\nThank you for your understanding.\\n\\nBest regards,\\nAdam', source_nodes=[], metadata=None)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent.chat(\"display the draft\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "72eb181f-9c17-4496-a98b-8fd484d087a5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== Calling Function ===\n",
      "Calling function: send_draft with args: {\n",
      "  \"draft_id\": \"r2727919118905591812\"\n",
      "}\n",
      "Got output: {'id': '18922adafcb185ed', 'threadId': '18922ad901074c7b', 'labelIds': ['SENT']}\n",
      "========================\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Response(response='The draft email has been sent successfully.', source_nodes=[], metadata=None)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent.chat(\"send the draft email\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
